﻿using HML.Design;
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing.Design;

namespace HML
{
    /// <summary>
    /// TabPagePlus集合
    /// </summary>
    [Editor(typeof(CollectionTextHelpEditor), typeof(UITypeEditor))]
    public class TabPagePlusCollection : IList
    {
        #region 字段

        private TabControlPlus owner;

        #endregion

        public TabPagePlusCollection(TabControlPlus owner)
        {
            this.owner = owner;
        }

        public virtual TabPagePlus this[int index]
        {
            get { return owner.GetTabPage(index); }
            set { this.owner.SetTabPage(index, value); }
        }

        object IList.this[int index]
        {
            get { return this[index]; }
            set
            {
                if (value is TabPagePlus)
                {
                    this[index] = (TabPagePlus)value;
                }
                else
                {
                    throw new ArgumentException("value");
                }
            }
        }

        public virtual TabPagePlus this[string key]
        {
            get
            {
                if (string.IsNullOrEmpty(key))
                {
                    return null;
                }

                int index = IndexOfKey(key);
                if (IsValidIndex(index))
                {
                    return this[index];
                }
                else
                {
                    return null;
                }

            }
        }

        [Browsable(false)]
        public int Count
        {
            get { return this.owner.TabCount; }
        }

        object ICollection.SyncRoot
        {
            get { return this; }
        }

        bool ICollection.IsSynchronized
        {
            get { return false; }
        }

        bool IList.IsFixedSize
        {
            get { return false; }
        }

        public bool IsReadOnly
        {
            get { return false; }
        }

        public void Add(TabPagePlus value)
        {
            if (value == null)
            {
                throw new ArgumentNullException("value");
            }

            owner.Controls.Add(value);
        }

        int IList.Add(object value)
        {
            if (value is TabPagePlus)
            {
                TabPagePlus tab_page = (TabPagePlus)value;
                this.Add(tab_page);
                return IndexOf(tab_page);
            }
            else
            {
                throw new ArgumentException("value");
            }
        }

        public void Add(string text)
        {
            TabPagePlus tab_page = new TabPagePlus();
            tab_page.Text = text;
            this.Add(tab_page);
        }

        public void Add(string key, string text)
        {
            TabPagePlus tab_page = new TabPagePlus();
            tab_page.Name = key;
            tab_page.Text = text;
            this.Add(tab_page);
        }

        public void AddRange(TabPagePlus[] tabPages)
        {
            if (tabPages == null)
            {
                throw new ArgumentNullException("pages");
            }
            foreach (TabPagePlus tab_page in tabPages)
            {
                this.Add(tab_page);
            }
        }

        public bool Contains(TabPagePlus tabPage)
        {
            if (tabPage == null)
                throw new ArgumentNullException("value");

            return IndexOf(tabPage) != -1;
        }

        bool IList.Contains(object tabPage)
        {
            if (tabPage is TabPagePlus)
            {
                return Contains((TabPagePlus)tabPage);
            }
            else
            {
                return false;
            }
        }

        public virtual bool ContainsKey(string key)
        {
            return IsValidIndex(IndexOfKey(key));
        }

        public int IndexOf(TabPagePlus tabPage)
        {
            if (tabPage == null)
            {
                throw new ArgumentNullException("value");
            }

            for (int index = 0; index < Count; ++index)
            {
                if (this[index] == tabPage)
                {
                    return index;
                }
            }
            return -1;
        }

        int IList.IndexOf(object tabPage)
        {
            if (tabPage is TabPagePlus)
            {
                return IndexOf((TabPagePlus)tabPage);
            }
            else
            {
                return -1;
            }
        }

        public virtual int IndexOfKey(String key)
        {
            if (string.IsNullOrEmpty(key))
            {
                return -1;
            }

            for (int i = 0; i < this.Count; i++)
            {
                if (((TabPagePlus)this.owner.TabPages[i]).Name == key)
                {
                    return i;
                }
            }

            return -1;
        }

        public void Insert(int index, TabPagePlus tabPage)
        {
            if (index < 0 || index > this.Count)
                return;

            owner.Controls.Add(tabPage);
            owner.Controls.SetChildIndex(tabPage, index);
        }

        void IList.Insert(int index, object tabPage)
        {
            if (tabPage is TabPagePlus)
            {
                this.Insert(index, (TabPagePlus)tabPage);
            }
            else
            {
                throw new ArgumentException("tabPage");
            }
        }

        public void Insert(int index, string text)
        {
            TabPagePlus tab_page = new TabPagePlus();
            tab_page.Text = text;
            this.Insert(index, tab_page);
        }

        private bool IsValidIndex(int index)
        {
            return ((index >= 0) && (index < this.Count));
        }

        public virtual void Clear()
        {
            owner.RemoveAll();
        }

        void ICollection.CopyTo(Array dest, int index)
        {
            if (Count > 0)
            {
                System.Array.Copy(owner.GetTabPages(), 0, dest, index, Count);
            }
        }

        public IEnumerator GetEnumerator()
        {
            TabPagePlus[] tabPages = owner.GetTabPages();
            if (tabPages != null)
            {
                return tabPages.GetEnumerator();
            }
            else
            {
                return new TabPagePlus[0].GetEnumerator();
            }
        }

        public void Remove(TabPagePlus value)
        {
            if (value == null)
                throw new ArgumentNullException("value");

            owner.Controls.Remove(value);
        }

        void IList.Remove(object value)
        {
            if (value is TabPagePlus)
            {
                this.Remove((TabPagePlus)value);
            }
        }

        public void RemoveAt(int index)
        {
            owner.Controls.RemoveAt(index);
        }

        public virtual void RemoveByKey(string key)
        {
            int index = IndexOfKey(key);
            if (IsValidIndex(index))
            {
                this.RemoveAt(index);
            }
        }

    }

}
